<?php
	/*==================================================================*/
	/*		文件名:MyDb.class.php                               */
	/*		概要: 数据访问层数据库处理的公共父类(模型).   	    */
	/*==================================================================*/
	class Model{
		protected $mysqli;
		protected $messList;
		protected $tabName;		//表的名称
		protected $fieldList;

		public function __construct() {
			$this->mysqli=new mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME);
			if (!$this->mysqli->set_charset("utf8")) {
			    printf("Error loading character set utf8: %s\n", $mysqli->error);
			} else {
			//    printf("Current character set: %s\n", $this->mysqli->character_set_name());
			}

            
            
            
            
            
			if(mysqli_connect_errno()) {
				echo "连接失败，原因为：".mysqli_connect_error();
				$this->mysqli=FALSE;
				exit();
			}		
		}

		//==========================================
		// 函数: add($postList)
		// 功能: 添加
		// 参数: $postList 提交的变量列表
		// 返回: 刚插入的自增ID
		//==========================================
		function add($postList) {
			$fieldList='';
			$value='';
			foreach ($postList as $k=>$v) {
				if(in_array($k, $this->fieldList)){
					$fieldList.=$k.",";
					if (!get_magic_quotes_gpc())
						$value .= "'".addslashes($v)."',";
					else
						$value .= "'".$v."',";
				}
			}

			$fieldList=rtrim($fieldList, ",");
			$value=rtrim($value, ",");

			$sql = "INSERT INTO {$this->tabName} (".$fieldList.") VALUES(".$value.")";
			pr($sql);
			$result=$this->mysqli->query($sql);

			if($result && $this->mysqli->affected_rows >0 ) 
				return $this->mysqli->insert_id;
			else
				return false;
		}


		//==========================================
		// 函数: mod($postList)
		// 功能: 修改表数据
		// 参数: $postList 提交的变量列表
		//==========================================
		function mod($postList) {
			$id=$postList["id"];
			unset($postList["id"]);
			$value='';
			foreach ($postList as $k=>$v) {
				if(in_array($k, $this->fieldList)){
					if (!get_magic_quotes_gpc())
						$value .= $k." = '".addslashes($v)."',";
					else
						$value .= $k." = '".$v."',";
				}
			}
			$value=rtrim($value, ",");
			$sql = "UPDATE {$this->tabName} SET {$value} WHERE id={$id}";
			return $this->mysqli->query($sql);	
		}
	
		//==========================================
		// 函数: del($id)
		// 功能: 删除
		// 参数: $id 编号或ID列表数组
		// 返回: 0 失败 成功为删除的记录数
		//==========================================
		function del($id) {
			if(is_array($id))
				$tmp = "IN (" . join(",", $id) . ")";
			else 
				$tmp = "= $id";
			
			$sql = "DELETE FROM {$this->tabName} WHERE id " . $tmp ;
			
			return $this->mysqli->query($sql);	
		
		}

		function total($where=""){
			$sql="SELECT id FROM {$this->tabName} {$where}";
			$result=$this->mysqli->query($sql);
			return $result->num_rows;	
		}
		function find($id) {	
			$filed=join(",", $this->fieldList);
			$sql="select {$filed} from {$this->tabName} where id={$id}";
			$result=$this->mysqli->query($sql);
			if($result && $result->num_rows >0){
				return $result->fetch_assoc();
			}else{
				return null;
			}
	
		}


		function findAll($field=array(), $offset=0, $num=0,$where="",$order="id desc"){
			if(empty($field)){
				$field=join(",", $this->fieldList);
			}else{
				$field=join(",", $field);
			}

			if($num==0){
				$sql="select {$field} from {$this->tabName} {$where}  order by {$order}";
			}else{
				$sql="select {$field} from {$this->tabName} {$where} order by {$order} limit {$offset},{$num}";
			}
			
			//pr($sql);
			$result=$this->mysqli->query($sql);

			if($result && $result->num_rows >0){
				$data=array();

				while($row=$result->fetch_assoc()){
					$data[]=$row;
				}

				return $data;
			}else{
				return null;
			}
		}


		public function close() {
			if($this->mysqli)
				$this->mysqli->close();
			$this->mysqli=FALSE;
		}

		function getMessList(){
			$message="";
			if(!empty($this->messList)){
				foreach($this->messList as $value){
					$message.=$value."<br>";
				}
			}
			return $message; 	
		}
	
		public function __destruct() {
			$this->close();
		}
	}

